<template>
  <div :class="['ui-relative', { 'no-radius': props.noRadius }]">
    <img :src="props.src" :alt="props.alt" @load="HideLoading" :style="{ width: props.width, height: props.height }" />
    <div v-if="showLoading" class="loading"><i></i></div>
  </div>
</template>

<script setup>
const props = defineProps({
  src: {
    type: String,
    default: ''
  },
  alt: {
    type: String,
    default: ''
  },
  noRadius: {
    type: Boolean,
    default: false
  },
  width: {},
  height: {}
});

let showLoading = ref(true);
const HideLoading = () => {
  showLoading.value = false;
};
</script>

<style lang="scss" scoped>
.ui-relative {
  border-radius: 20px;
  overflow: hidden;
  cursor: pointer;
  &.no-radius {
    border-radius: 0;
  }
}
img {
  display: block;
  max-width: 100%;
  object-fit: cover;
}
.loading {
  background-color: rgba(0, 0, 0, 0.5);
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  i {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 50px;
    height: 50px;
    @include bgImg('../assets/icon-loading.png');
    animation: rotate infinite 3s linear;
  }
}
@keyframes rotate {
  0% {
    transform: translate(-50%, -50%) rotate(0deg);
  }
  50% {
    transform: translate(-50%, -50%) rotate(180deg);
  }
  100% {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}
</style>
